package com.fy.common.config;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

@Component
public class TimestampConverter implements Converter<String, Timestamp> {
  @Override
  public Timestamp convert(String source) {
    // 处理空值
    if (source == null || source.isEmpty()) {
      return null;
    }

    try {
      // 尝试将时间戳转换为Timestamp
      long timestamp = Long.parseLong(source);
      return new Timestamp(timestamp);
    } catch (NumberFormatException e) {
      try {
        // 如果不是时间戳，尝试解析日期时间字符串
        return Timestamp.valueOf(source);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException("Invalid timestamp format: " + source);
      }
    }
  }
}